//
//  ZDLeftCategoryController.m
//  ZDPension
//
//  Created by 刘甲奇 on 2017/6/14.
//  Copyright © 2017年 maker. All rights reserved.
//

#import "ZDLeftCategoryController.h"
#import "ZDSimpleCategoryModel.h"
#import "ZDLeftCategoryCell.h"

static NSString *const ZDLeftCategoryCellId = @"ZDLeftCategoryCell";

@interface ZDLeftCategoryController ()

/**
 <#Description#>
 */
@property(nonatomic,strong)ZDSimpleCategoryModel *selectModel;

@end

@implementation ZDLeftCategoryController

- (void)setupUI
{
    [super setupUI];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZDLeftCategoryCell" bundle:nil] forCellReuseIdentifier:ZDLeftCategoryCellId];
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [self resetTablewFrame:self.view.bounds];
}
- (void)loadData
{
    [self requestRefreshData];
}
#pragma mark- 网络请求
- (void)requestRefreshData
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:self.model.gc_id forKey:@"gc_id"];
    
    [SVProgressHUD showWithStatus:@"正在加载中"];
    __weak typeof (self) weakSelf = self;
    [API netAccess:MainGetServiceList andMethor:@"GET" andDict:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        ZDLog(@"%s---%d---%@",__func__,__LINE__,successCode);
        NSDictionary *datas = successCode[@"datas"];
        NSString *error = datas[@"error"];
        if (error)
        {
            [SVProgressHUD showInfoWithStatus:@"暂无数据"];
        }else
        {
            NSMutableArray *goodsListDict = datas[@"class_list"];
            NSArray *tempModel = [ZDSimpleCategoryModel mj_objectArrayWithKeyValuesArray:goodsListDict];
            [weakSelf.dataSource removeAllObjects];
            [weakSelf.dataSource addObjectsFromArray:tempModel];
            if (weakSelf.dataSource.count>0)
            {
                ZDSimpleCategoryModel *first = weakSelf.dataSource[0];
                first.isSeleted = YES;
                weakSelf.selectModel = first;
                [weakSelf.dataSource replaceObjectAtIndex:0 withObject:first];
                [weakSelf.tableView reloadData];
                //通知右侧的
                [weakSelf.delegate categoryControllerSelected:first.gc_id];
                
            }else
            {
                [SVProgressHUD showInfoWithStatus:@"暂未该服务"];
            }
        }
        
        
    } falure:^(NSError *er) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showInfoWithStatus:@"网络错误"];
    }];
}
#pragma mark- tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZDLeftCategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:ZDLeftCategoryCellId forIndexPath:indexPath];
    ZDSimpleCategoryModel *model = self.dataSource[indexPath.row];
    model.indexPath = indexPath;
    cell.model = model;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZDSimpleCategoryModel *willModel = self.dataSource[indexPath.row];
    if ([willModel isEqual:self.selectModel])
    {
        return;
    }
    willModel.isSeleted = YES;
    self.selectModel.isSeleted = NO;
    [self.tableView reloadRowsAtIndexPaths:@[indexPath,self.selectModel.indexPath] withRowAnimation:UITableViewRowAnimationFade];
    self.selectModel = willModel;
    
    //通知右侧
    [self.delegate categoryControllerSelected:willModel.gc_id];
}

@end
